<template>
  <div>
    <el-button size="mini" type="primary" icon="el-icon-star-off" @click="to" />
    <el-dialog :append-to-body="true" :visible.sync="dialog" title="出库" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="备件编号" >
        <el-input v-model="form.spareno" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="名称" >
        <el-input v-model="form.sparepartname" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="类型" >
        <el-input v-model="form.typename" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="出库用途" >
        <el-input v-model="form.purpose" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="领用人" >
        <el-input v-model="form.usedBy" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="出库人" >
        <el-input v-model="form.retrievalBy" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="出库时间" >
        <!-- <el-input v-model="form.retrievalTime" style="width: 370px;"/> -->
        <el-date-picker
            v-model="form.retrievalTime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
  </div>
</template>
<script>
import { add, edit } from '@/api/tSparepartRetrieval'
import { parseTime } from '@/utils/index'
export default {
  props: {
    data: {
      type: Object,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      form: {
        id: '',
        spareno: '',
        sparepartname: '',
        typename: '',
        purpose: '',
        usedBy: '',
        retrievalBy: '',
        retrievalTime: ''
      },
      rules: {
      }
    }
  },
  methods: {
    to() {
      console.log(this.data)
      if(this.data.state === 1){
        this.$notify({
          title: '该配件已出库！',
          type: 'success',
          duration: 2500
        })
        return ;
      }
      this.dialog = true
      this.form.spareno = this.data.spareno
      this.form.sparepartname = this.data.sparepartname
      this.form.typename = this.data.typename
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
        this.doAdd()
    },
    doAdd() {
      this.form.retrievalTime = parseTime(this.form.retrievalTime)
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '出库成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        spareno: '',
        sparepartname: '',
        typename: '',
        purpose: '',
        usedBy: '',
        retrievalBy: '',
        retrievalTime: ''
      }
    }
  }
}
</script>

<style scoped>
div {
  display: inline-block;
  margin-right: 3px;
}
</style>
